package mi.payment.transaction.sale.code;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import lainey.ui.utils.LogUtil;
import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.constants.Configs;
import mi.payment.controller.BaseController;
import mi.payment.db.PayDetailModel;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.PageRoute;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.transaction.PollingTask;
import mi.payment.utils.transaction.QRCodeConsumptionTask;
import mi.payment.utils.transaction.QRCodeLoopingTask;
import sunmi.payment.ui.dialog.LoadDialog;

/**
 * 扫码支付帮助类，封装扫码支付的公共逻辑
 */
public class ScanPaymentController extends BaseController {

    private static final int TIME_OUT = 90;     // 超时时间
    private static final int RETRY_COUNT = 90;  // 重试次数

    private TradeData mReqTradeData;
    private PayDetail mReqPayDetail;

    private PollingTask mPollingTask;

    public ScanPaymentController(ParentActivity activity, TradeData tradeData) {
        super(activity);
        mReqTradeData = tradeData;
        mReqPayDetail = mReqTradeData.getPayDetail();
        mReqPayDetail.originalBathNo = mReqPayDetail.getBathNo();
        mReqPayDetail.originalPOSNum = mReqPayDetail.getVoucherNo();
    }

    @SuppressLint("StaticFieldLeak")
    public void scanSuccess() {
        // 消费
        mReqTradeData.getPayDetail().setTransactionType(1);
        QRCodeConsumptionTask task = new QRCodeConsumptionTask(mContext) {

            @Override
            protected void onPostExecuteImpl(TradeData tradeData) {
                PayDetail resPay = tradeData.getPayDetail();
                String answerCode = resPay.getTradeAnswerCode();
                int qrCodeTransState = resPay.getQRCodeTransState();
                boolean isSuccess = TextUtils.equals("00", answerCode) && qrCodeTransState == 1;
                if (isSuccess) {
                    PageRoute.startAction(mActivity, PageRoute.TYPE_QR_CODE_CONSUME, resPay);
                } else if (qrCodeTransState == 2) {
                    LogUtil.e(Configs.TAG, "支付中或状态不明");
                    handlePaying();
                } else {
                    PageRoute.startFailAction(mActivity, PageRoute.TYPE_QR_CODE_CONSUME, resPay);
                }
            }

            @Override
            protected void onTaskTimeoutImpl() {
                PageRoute.startTimeoutAction(mActivity, PageRoute.TYPE_QR_CODE_CONSUME, mReqTradeData.payDetail);
            }

        };
        LoadDialog dialog = mActivity.getLoadingDialog(R.string.load_connecting);
        task.setLoadDialog(dialog);
        task.execute(mReqTradeData);
    }

    @SuppressLint("StaticFieldLeak")
    private void handlePaying() {
        QRCodeLoopingTask task = new QRCodeLoopingTask(mActivity) { };
        mPollingTask = new PollingTask(RETRY_COUNT, task) {

            @Override
            protected void onPostExecuteImpl(TradeData tradeData) {
                PayDetail resPay = tradeData.getPayDetail();

                mReqPayDetail.setReferNo(resPay.referNo);
                mReqPayDetail.setThirdTransNum(resPay.thirdTransNum);
                mReqPayDetail.setTradeResultDes(resPay.tradeResultDes);
                mReqPayDetail.setTradeAnswerCode(resPay.tradeAnswerCode);
                mReqPayDetail.setQRCodeTransState(resPay.QRCodeTransState);
                // 更新数据库
                PayDetailModel.getInstance().update(mReqPayDetail);

                PageRoute.startAction(mActivity, PageRoute.TYPE_QR_CODE_CONSUME, resPay);
            }

            @Override
            protected void onTaskTimeoutImpl() {
                PageRoute.startFailAction(mActivity, PageRoute.TYPE_QR_CODE_CONSUME, mReqTradeData.payDetail);
            }

        };
        LoadDialog dialog = mActivity.getLoadingDialog(R.string.load_search);
        mPollingTask.setLoadDialog(dialog);
        mPollingTask.setTaskTimeout(TIME_OUT);
        mPollingTask.execute(mReqTradeData);
    }

    public void onActivityDestroy() {
        if (mPollingTask != null) {
            mPollingTask.forceStopPolling();
            mPollingTask = null;
        }
    }

}
